﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using Ajeelee.Core.Utils;
using FxhjpgWeb.Model;
using FxhjpgWeb.Business;
using FxhjpgWeb.WebSite.Code;
using System.IO;
using System.Collections;

namespace FxhjpgWeb.WebSite.WebAdmin.ajax
{
    /// <summary>
    /// DataAjax 的摘要说明
    /// </summary>
    public class DataAjax : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            NameValueCollection dictionarySource =
              (context.Request.RequestType.ToUpper().Equals("GET"))
              ? context.Request.QueryString : context.Request.Form;
            NameValueCollection Nvc = new NameValueCollection(dictionarySource);
            switch (Nvc["act"])
            {
                case "DelPhotoImg": //删除图片
                    context.Response.Write(DelPhotoImg(Nvc["Ids"]));
                    break;
                case "BatchSavePhotoImgs":  // 保存图片信息
                    context.Response.Write(BatchSavePhotoImgs(Nvc["Id"], Nvc["Summary"], Nvc["Summary_En"], Nvc["Url"], Nvc["Sort"]));
                    break;
            }
        }

        IPhotoImgsBusiness photoBll = new PhotoImgsBusinessFactory().CreatePhotoImgsBusiness();
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private string DelPhotoImg(string ids)
        {
            if (ids.IsNullOrEmpty())
            {
                return "{\"result\":\"false\",\"prompt\":\"参数不完整(缺少ID)！\"}";
            }
            try
            {
                foreach (string id in ids.Split(","))
                {
                    if (id.ToInt() > 0)
                    {
                        PhotoImgs info = photoBll.GetById(id.ToInt());
                        if (info != null)
                        {
                            ManagedThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DelPhotoImg), new string[] { info.BigSrc, info.SmallSrc });
                        }
                        photoBll.Delete(id.ToInt());
                        return "{\"result\":\"true\",\"prompt\":\"\"}";
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
                return "{\"result\":\"false\",\"prompt\":\"操作失败,处理数据时出错，请重新操作或联系管理员！\"}";
            }
            return "{\"result\":\"false\",\"prompt\":\"操作失败，请重新操作！\"}";
        }

        /// <summary>
        /// 后台删除图片
        /// </summary>
        /// <param name="_imgs"></param>
        private void DelPhotoImg(object _imgs)
        {
            string[] imgs = _imgs as string[];
            if (imgs != null && imgs.Length > 0)
            {
                foreach (string img in imgs)
                {
                    if (img.IsNotNullAndEmpty())
                    {
                        string _imgPath = Function.GetMapPath(img);
                        if (File.Exists(_imgPath))
                        {
                            File.Delete(_imgPath);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 保存图片信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private string BatchSavePhotoImgs(string id, string summary,string summaryen, string linkUrl, string sort)
        {
            if (id.IsNullOrEmpty() || id.ToInt() <= 0)
            {
                return "{\"result\":\"false\",\"prompt\":\"参数不完整(缺少ID)！\"}";
            }
            try
            {
                Hashtable table = new Hashtable();
                table.Add(PhotoImgsHash.Id, id.ToInt());
                table.Add(PhotoImgsHash.Summary, summary);
                table.Add(PhotoImgsHash.Summary_En, summaryen);
                table.Add(PhotoImgsHash.LinkUrl, linkUrl);
                table.Add(PhotoImgsHash.Sort, sort.ToInt(0));
                photoBll.UpdateSomeVal(table);

                return "{\"result\":\"true\",\"prompt\":\"\"}";
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
                return "{\"result\":\"false\",\"prompt\":\"操作失败,处理数据时出错，请重新操作或联系管理员！\"}";
            }
            return "{\"result\":\"false\",\"prompt\":\"操作失败，请重新操作！\"}";
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}